# -*- coding: utf-8 -*-
#
# Copyright 2009 Vanderbilt University
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 

"""Simplified library for plotting information.  Reduces interactions to
as single call.

.. moduleauthor:: John Paulett <john.paulett -at- vanderbilt.edu>
"""
import matplotlib.pyplot as plt

def show():
    """Display the current figure buffer in a GUI window."""
    plt.show()

def xyscatter(x, y, fmt='.', xlabel=None, ylabel=None, title=None, filename=None):
    """Draw a simple XY scatter plot and optionally saves the plot to *filename*
    if provided."""
    fig, ax = create_figure(xlabel, ylabel, title)
    ax.plot(x, y, fmt)
    save(fig, filename)
    return fig

def loglog(x, y, fmt='.', xlabel=None, ylabel=None, title=None, filename=None):
    """Returns a log-log plot and optionally saves the plot to *filename*
    if provided.
    """
    fig, ax = create_figure(xlabel, ylabel, title)
    ax.loglog(x, y, fmt)
    save(fig, filename)
    return fig

def create_figure(xlabel=None, ylabel=None, title=None):
    """Creates a new figure object. The figure object and the axis of the 
    figure are returned as a tuple.
    """
    fig = plt.figure()
    ax = fig.add_subplot(111)
    if xlabel: 
        ax.set_xlabel(xlabel)
    if ylabel: 
        ax.set_ylabel(ylabel)
    if title: 
        ax.set_title(title)
    return fig, ax

def save(fig, filename):
    """Saves a figure object, *fig*, to the location specified by *filename*.
    The extension of *filename* will inform the formatting of the image.
    """  
    if filename:  
        fig.savefig(filename, dpi=200)
    